package frame;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private MainDesktop desktop = new MainDesktop();

	public MainFrame() {
		setSize(S_WIDTH, S_HEIGHT);
		setResizable(false);

		initFrameLocation();

		add(desktop, BorderLayout.CENTER);

		setTitle("QQ Snooker Cheater");

		setDefaultCloseOperation(MainFrame.DO_NOTHING_ON_CLOSE);
		setVisible(true);

		addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {
				if (desktop.getStatus() == MainDesktop.FINISH_STATUS) {
					desktop.resetStatus();
				}
			}

			public void mouseEntered(MouseEvent arg0) {
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}
		});

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(null, "是否关闭所有QQ游戏客户端程序？",
						"询问", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					// closeGameByForce("PocketRPG.exe");
					closeGameByForce("QQGame*");
				}

				setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
			}
		});
	}

	private void initFrameLocation() {
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;

		setLocation((width - S_WIDTH) / 2, (height - S_HEIGHT) / 2);
	}

	private void closeGameByForce(String name) {
		String[] cmdParams = { "taskkill", "/F", "/FI", "IMAGENAME eq " + name,
				"/T" };
		try {
			Runtime.getRuntime().exec(cmdParams).waitFor();
		} catch (Exception exp) {
		}
	}

	private static final int S_WIDTH = 222;

	private static final int S_HEIGHT = 134;

}
